﻿/**********************************************************************************
 * Description:     
 * MemberVariable:
 * Functions:       
 * Modify information:     
 * 2008-8-5     New           DuYaoHui     Create Class
 * 
 * ********************************************************************************/
using System;
using System.Data;
using System.Web.UI.WebControls;
using Fjaspx.Sys.Bll;

namespace Fjaspx.UI.Web
{
    public partial class Administrator_Objet_admin : AdminPage
    {
        private int _pages;
        /// <summary>
        /// Gets or sets the pages.
        /// </summary>
        /// <value>The pages.</value>
        public int pages
        {
            set { _pages = value; }
            get { return _pages; }
        }
        #region ClassKind属性
        protected int ClassKind
        {
            get
            {
                return (int)ViewState["ClassKind"];
            }
            set
            {
                ViewState["ClassKind"] = value;
            }
        }
        #endregion
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                //try
                //{  
                    this.ClassKind = Convert.ToInt32(Request.QueryString["ClassKind"]);
                    DdlMenu_bind();

                    if (Request["role"] != null)
                    {
                        Repeater_admin_bind(Request["Class"],null);
                    }
                    else
                    {
                        if (this.keywords.Value != null)
                        {
                            Repeater_admin_bind(DdlMenu.SelectedValue.Trim(), this.keywords.Value);
                        }
                        else { Repeater_admin_bind(DdlMenu.SelectedValue.Trim(), null); }
                    }


                    switch (Request["t"])
                    {
                        case "del":
                            del_news(Convert.ToInt32(Request["id"]));
                            Repeater_admin_bind(DdlMenu.SelectedValue.Trim(),null);
                            break;
                        case "ding":
                            if (Request["ding"] == "1") { Ding_news(Convert.ToInt32(Request["id"]), 0); }
                            else { Ding_news(Convert.ToInt32(Request["id"]), 1); }
                            Repeater_admin_bind(DdlMenu.SelectedValue.Trim(),null);
                            break;
                        case "tuijian":
                            if (Request["tuijian"] == "1") { Tuijian_news(Convert.ToInt32(Request["id"]), 0); }
                            else { Tuijian_news(Convert.ToInt32(Request["id"]), 1); }
                            Repeater_admin_bind(DdlMenu.SelectedValue.Trim(),null);
                            break;
                        case "K":
                            if (Request["K"] == "0") { K_news(Convert.ToInt32(Request["id"]), 1); }
                            else { K_news(Convert.ToInt32(Request["id"]), 0); }
                            Repeater_admin_bind(DdlMenu.SelectedValue.Trim(),null);
                            break;
                    }

                //}
                //catch { Response.Redirect("/administrator/Message.aspx?message=数据绑定错误！&z=no"); }
            }
        }
        /// <summary>
        /// Repeater_admin_binds the specified classs.
        /// </summary>
        /// <param name="classs">The classs.</param>
        void Repeater_admin_bind(string classs,string keyword)
        {

            int page = 0;
            string where = "";
            if (Request["page"] != null && Request["page"]!="")
            {
                page = Convert.ToInt32(Request["page"]) - 1;
            }

            if (keyword != null)
            {
                where = " title like '%" + keyword.Trim() + "%' ";
            }

            if (classs != "")
            {
                where = " classname =" + classs;
            }
            if (Request["day"] != null) { where = " datediff(ss,time,getdate())<=" + (Convert.ToInt32(Request["day"])*24).ToString() + "*60*60 "; }
            
            if (where != "")
            {
                /////////////////////////
                if (Request["role"] != null && Fjaspx.Common.Session.Get("user")!=null) { where = where + " and author = '" + Fjaspx.Common.Session.Get("user") + "' "; }
                where = where + " and type = '" + this.ClassKind.ToString() + "' ";
            }
            else { where = " type = '" + this.ClassKind.ToString() + "' ";
            if (Request["role"] != null && Fjaspx.Common.Session.Get("user") != null) { where = where + " and author = '" + Fjaspx.Common.Session.Get("user") + "' "; }
            }

            ROYcms_news Bll = new ROYcms_news();
            pages = Bll.GetCount(where) / 18 + 1; //分页的总数          
            PagedDataSource pg = new PagedDataSource();
            pg.DataSource = Bll.GetList(where+" order by bh desc ").Tables[0].DefaultView;
            pg.AllowPaging = true;
            pg.PageSize = 18;
            pg.CurrentPageIndex = page;
            Repeater_admin.DataSource = pg;
            Repeater_admin.DataBind();
        }


        #region 数据绑定
        void DdlMenu_bind()
        {

            //this.btnAdd.Attributes.Add("onclick", "return ChkInput()");
            //this.btnEdit.Attributes.Add("onclick", "return ChkInput()");

            DataSet ds = new Fjaspx.Sys.Bll.ROYcms_class().GetClassList(this.ClassKind);

            //this.rptMenuList.DataSource = ds.Tables[0].DefaultView;
            //this.rptMenuList.DataBind();

            this.DdlMenu.Items.Clear();
            this.DdlMenu.Items.Add(new ListItem("请选择所属分类", ""));
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                int ClassTj = Convert.ToInt32(dr["ClassTj"]);
                string ClassId = dr["Id"].ToString().Trim();
                string ClassName = dr["ClassName"].ToString().Trim();

                if (ClassTj == 1)
                {
                    this.DdlMenu.Items.Add(new ListItem(ClassName, ClassId));

                }
                else
                {
                    ClassName = "├ " + ClassName;
                    ClassName = Fjaspx.Common.StringPlus.StringOfChar(ClassTj - 1, "　") + ClassName;

                    this.DdlMenu.Items.Add(new System.Web.UI.WebControls.ListItem(ClassName, ClassId));
                }
            }

        }
        #endregion

        /// <summary>
        /// Del_newses the specified bh.
        /// </summary>
        /// <param name="bh">The bh.</param>
        void del_news(int bh)
        {
            //更新静态生成文件
            NewHtml(null, Convert.ToInt32(Request["class"]));
            //删除新闻生成文件
            new Fjaspx.Templet.NewShow().DelHtml(bh);

            ROYcms_news BLL = new ROYcms_news();
            BLL.Delete(bh);

        }
        //  
        /// <summary>
        /// K_newses the specified bh.控制开关 打开 和关闭一条数据
        /// </summary>
        /// <param name="bh">The bh.</param>
        /// <param name="on_of">The on_of.</param>
        void K_news(int bh, int on_of)
        {
            ROYcms_news BLL = new ROYcms_news();
            BLL.K_news(bh, on_of);
        }
       
        /// <summary>
        /// Ding_newses the specified bh. //修改一条数据是否置顶  推荐
        /// </summary>
        /// <param name="bh">The bh.</param>
        /// <param name="ding">The ding.</param>
        void Ding_news(int bh, int ding)
        {

            ROYcms_news BLL = new ROYcms_news();
            BLL.Ding_news(bh, ding);
        }
        /// <summary>
        /// Tuijian_newses the specified bh.
        /// </summary>
        /// <param name="bh">The bh.</param>
        /// <param name="tuijian">The tuijian.</param>
        void Tuijian_news(int bh, int tuijian)
        {

            ROYcms_news BLL = new ROYcms_news();
            BLL.Tuijian_news(bh, tuijian);
        }
        /// <summary>
        /// Handles the Click event of the ImageButton_all_del control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.ImageClickEventArgs"/> instance containing the event data.</param>
        protected void ImageButton_all_del_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            //批量删除
            int nub = 0;
            for (int i = 0; i < Repeater_admin.Items.Count; i++)
            {
                int id = Convert.ToInt32(((Label)Repeater_admin.Items[i].FindControl("Label_id")).Text);
                CheckBox CK = (CheckBox)Repeater_admin.Items[i].FindControl("ID_list");
                if (CK.Checked)
                {
                    del_news(id);
                    nub += 1;
                }
            }
            Response.Redirect("/administrator/Message.aspx?message=批量删除<b>" + nub + "</b>条信息成功！&z=yes");
        }
        /// <summary>
        /// Handles the Click event of the ImageButton_ding control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.ImageClickEventArgs"/> instance containing the event data.</param>
        protected void ImageButton_ding_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            //批量推荐
            int nub = 0;
            for (int i = 0; i < Repeater_admin.Items.Count; i++)
            {
                int id = Convert.ToInt32(((Label)Repeater_admin.Items[i].FindControl("Label_id")).Text);
                CheckBox CK = (CheckBox)Repeater_admin.Items[i].FindControl("ID_list");
                if (CK.Checked)
                {
                    Tuijian_news(id, 0);
                    nub += 1;
                }
            }
            Response.Redirect("/administrator/Message.aspx?message=批量推荐<b>" + nub + "</b>条信息成功！&z=yes");
        }
        /// <summary>
        /// Handles the Click event of the ImageButton_K control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.ImageClickEventArgs"/> instance containing the event data.</param>
        protected void ImageButton_K_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            //批量验证
            int nub = 0;
            for (int i = 0; i < Repeater_admin.Items.Count; i++)
            {
                int id = Convert.ToInt32(((Label)Repeater_admin.Items[i].FindControl("Label_id")).Text);
                CheckBox CK = (CheckBox)Repeater_admin.Items[i].FindControl("ID_list");
                if (CK.Checked)
                {
                    K_news(id, 0);
                    nub += 1;
                }
            }
            Response.Redirect("/administrator/Message.aspx?message=批量验证<b>" + nub + "</b>条信息成功！&z=yes");
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the DdlMenu control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void DdlMenu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DdlMenu.SelectedValue != "")
            {
                Repeater_admin_bind(DdlMenu.SelectedValue.Trim(),null);
            }

        }

        /// <summary>
        /// Get_classnames the specified id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public string get_classname(int id) {

            Fjaspx.Sys.Bll.ROYcms_class bll = new ROYcms_class();
            
            return bll.GetClassTitle(id);
        
        }
        /// <summary>
        /// 生成静态HTML文件 News the HTML.
        /// </summary>
        public void NewHtml(int? bh, int classname)
        {
            if (Config.ROYcmsConfig.Get_Config().HTML == "true")
            {
                Fjaspx.Templet.NewIndex NewIndex = new Fjaspx.Templet.NewIndex();
                Fjaspx.Templet.NewList NewList = new Fjaspx.Templet.NewList();
                Fjaspx.Templet.NewShow NewShow = new Fjaspx.Templet.NewShow();
                Fjaspx.Sys.Bll.ROYcms_class BLL = new Fjaspx.Sys.Bll.ROYcms_class();
                Fjaspx.Sys.Model.ROYcms_class model = BLL.GetModel(BLL.GetClassId(Convert.ToInt32(classname)));

                NewList.classname = classname;
                NewList.onepage = 30;
                NewList.NewHtml(model);

                NewList.NewAllHtml(model.ClassKind);

                NewShow.bh = bh;
                NewShow.NewHtml(model);

                NewIndex.NewHtml();
            }
        }

        protected void Submit_Click(object sender, EventArgs e)
        {
             Repeater_admin_bind("",this.keywords.Value);
        }


    }
}